﻿ using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NLog.Fluent;
using S.Common.Enums.Archives;
using S.EarTag.API.Application.MediatorCommand.EarTagCommand;
using S.EarTag.Domain.Model;
using S.EarTag.Read.API.Application.MediatorCommand.EarTagCommand;
using S.EarTag.Read.API.Mediator.MediatorCommand;
using S.EarTag.Read.API.Mediator.MediatorCommand.EarTagCommand;

namespace S.EarTag.Read.API.Controllers
{
    /// <summary>
    /// 耳标管理——归光阳
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class EarTagController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<EarTagController> _logger;
        public EarTagController(IMediator mediator, ILogger<EarTagController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }
        /// <summary>
        /// 根据Ids进行批量查询
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> GetEarTagIds(string? ids)
        {
            GetGetIdsCommand command = new GetGetIdsCommand();
            command.ids=ids;
            var list = await _mediator.Send(command);
            return Ok(list);
        }
        /// <summary>
        /// 测试异常记录日志
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAsync()
        {
            try
            {
                _logger.LogInformation("cs1");
                return Ok(Convert.ToInt32("123asa"));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message + "请求出错");
                throw;
            }
        }
        /// <summary>
        /// 查询状态为false的可视耳号
        /// </summary>
        /// <returns></returns>
        [RequestSizeLimit(long.MaxValue)]//设置请求数量为long的最大值
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> GetEarTagListstate()//int pageSize=100
        {
            //string rootPath = AppDomain.CurrentDomain.BaseDirectory;
            //string filePath = Path.Combine(rootPath, "example.txt");
            //using (StreamWriter writer = new StreamWriter(filePath))
            //{
            //    writer.WriteLine("报错异常" + ex.Message);

            //    writer.Close();
            //}
            try
            {
                //int pageIndex = 1;
                EarTagGetEarTagCommand command = new EarTagGetEarTagCommand();
                //command.pageIndex= pageIndex;
                //command.pageSize= pageSize;
                var list = await _mediator.Send(command);
                
                return Ok(list);
            }
            catch (Exception ex)
            {
               
                _logger.LogInformation("写入文本文件"+ ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 根据Id查询耳标
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> GetEarTagById(int id)
        {
            EarTagByIdCommand command=new EarTagByIdCommand();
            command.id= id;
            var result=await _mediator.Send(command);
            return Ok(result);
        }
        /// <summary>
        /// 耳标多条件查询+分页显示
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="EarTag"></param>
        /// <param name="BreedItem"></param>
        /// <param name="BirthState"></param>
        /// <param name="BindState"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> GetEarTagListPage(int pageIndex, int pageSize, string? EarTag, int BreedItem, string? BirthState, string? BindState)
       {
            EarTagFindListCommand command=new EarTagFindListCommand();
            command.pageIndex= pageIndex;
            command.pageSize= pageSize;
            command.EarTag = EarTag;
            command.BreedItem = BreedItem;
            command.BirthState = BirthState;
            command.BindState = BindState;
            var result = await _mediator.Send(command);
            return Ok(result);
        }
        /// <summary>
        /// 条件筛选耳标状态为未使用的耳标
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> GetEarTagList()
        {
            EarTagSeachCommand command = new EarTagSeachCommand();
            var rest = await _mediator.Send(command);
            return Ok(rest);
        }
        /// <summary>
        /// 查询耳标状态为已使用
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> GetTarTag()
        {
            EarTagQueryCommand command =new EarTagQueryCommand();
            var rest=await _mediator.Send(command);
            return Ok(rest);
        }
        /// <summary>
        /// 查询枚举品种类型
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GetBreedList()
        {
            List<object> idlist= new List<object>();
            foreach (var item in Enum.GetValues<VarietyEnum>())
            {
                idlist.Add(new
                {
                    id = item,
                    name = item.ToString(),
                });
            }
            return Ok(idlist);
        }
    }
}
